package com.nl.controller.web.service;

import java.util.*;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement.Else;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.mapper.CommonMapper;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.CRMOpenInfo;
import com.nl.controller.web.bean.Send5gResult;
import com.nl.controller.web.bean.SmsMgrGovernmentInfo;
import com.nl.controller.web.bean.SmsMgrInfo;
import com.nl.controller.web.bean.CRMFindMgr.MgrInfo;
import com.nl.controller.web.global.DealSendResultCLQ;
import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.web.mapper.CRMMapper;
import com.nl.controller.web.mapper.Sms5gMapper;
import com.nl.controller.web.redis.RedisCluster;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class Sms5gService {
	
	@Autowired
	private Sms5gMapper sms5gMapper;

	public JSONObject templateCheckResult(String templateCheckResultInfo) {
		
			String resp="";
	        JSONObject retMap=new JSONObject();
	        try{
		        if(templateCheckResultInfo!=null&&!"".equals(templateCheckResultInfo)){
		        	JSONObject jsonObject=JSONObject.parseObject(templateCheckResultInfo);
		        	String modeId = jsonObject.getString("modeId");
		            log.info("【5G平台模板审核结果通知】模版编号：{}",modeId);
		            
		            //0、失效 1、未审核 2、审核成功 3、审核中 4、审核失败
		            String ydStatus=jsonObject.getString("ydStatus"); 
		            log.info("【5G平台模板审核结果通知】移动审核状态：{}",ydStatus);
		            String ydReason = jsonObject.getString("ydReason");
		            log.info("5G平台模板审核结果通知】移动审核理由：{}",ydReason);
		            
		            if("4".equals(ydStatus)){
		            	ydStatus = "3";
		            }else if("1".equals(ydStatus)){
		            	ydStatus = "0";
		            }
		            
//		            String ltStatus=jsonObject.getString("ltStatus");      
//		            log.info("【5G平台模板审核结果通知】联通审核状态：{}",ltStatus);
//		            String ltReason = jsonObject.getString("ltReason");
//		            log.info("5G平台模板审核结果通知】联通审核拒绝原因：{}",ltReason);
//		            
//		            String dxStatus=jsonObject.getString("dxStatus");      
//		            log.info("【5G平台模板审核结果通知】电信审核状态：{}",dxStatus);
//		            String dxReason = jsonObject.getString("dxReason");
//		            log.info("5G平台模板审核结果通知】电信审核拒绝原因：{}",dxReason);
		            
		            int updateResult = sms5gMapper.updateTemplateCheckState(modeId, ydStatus, ydReason);
		            if(updateResult>0){
		            	resp = "SUCCESS";
		            }else{
		            	resp = "FAIL";
		            	
		            }
		            retMap.put("resp", resp);
		        	}
	        }catch(Exception e) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				resp = "5G平台模板审核结果通知发生异常";
				e.printStackTrace();
				retMap.put("resp", resp);
			}
	        return retMap;
	}

	public JSONObject smsSendResult(String smsSendResult) {
		
		String resp="SUCCESS";
        JSONObject retMap=new JSONObject();
        //RedisCluster redisCluster=null;
        try{
	        if(smsSendResult!=null&&!"".equals(smsSendResult)){
	        	//redisCluster=new RedisCluster(GlobalConst.REDIS_CONFIG);
	        	log.info("5g send result req:"+smsSendResult);
	        	Send5gResult send5gResult=JSON.parseObject(smsSendResult,Send5gResult.class);
	        	//String modeId = jsonObject.getString("modeId");
	        	String taskId = send5gResult.getTaskId();
	            
	            
	            //发送结果 4 成功 5 失败 6 下载成功
	            //String status=jsonObject.getString("status");
	            String status = send5gResult.getStatus();
	            
	            //String mobile=jsonObject.getString("mobile");   
	            String mobile = send5gResult.getMobile();
	            
	            //String errorCode=jsonObject.getString("errorCode");   
	            String errorCode = send5gResult.getErrorCode();
	            //redisCluster.set(modeId+mobile, status);
	            
	            log.info("【5G平台发送结果通知】taskId任务编号:"+taskId+",发送号码："+mobile+",发送结果："+status+",错误编码："+errorCode);
	            
	            //发送默认成功，若发送成功则不处理发送结果
	            if(!"4".equals(status)){
	            	if("5".equals(status)){
	            		status = "-1";
	            		send5gResult.setStatus(status);
	            	}
	            	DealSendResultCLQ.sendResultDealQueue.add(send5gResult);
	            }
	            
	            retMap.put("resp", resp);
	        	}
        }catch(Exception e) {
			resp = "5G平台模板审核结果通知发生异常";
			e.printStackTrace();
			retMap.put("resp", resp);
		}
        return retMap;
	}
	
	
	

}
